/**
 * Just some examples.
 * 
 * @author back2dos
 */
import com.robertpenner.easing.Bounce;
import com.robertpenner.easing.Linear;
class Examples {
	/**
	 * shows all examples
	 */
	static public function all():Void {
		Examples.squares(Minion.bind(Examples.bezier, null, Examples.penner));
	}
	static private var layer:MovieClip;
	/**
	 * This one makes squares dance. Yeeehaaaaw!
	 */
	static public function squares(onDone:Function):Void {
		layer.removeMovieClip();
		layer = _root.createEmptyMovieClip('layer', _root.getNextHighestDepth());
		Util.plot(Minion.EASE, layer, 200, 200, 10, 210);
		var mc1 = makeSquare(), mc2 = makeSquare(), mc3 = makeSquare(), mc4 = makeSquare();			
		var right = { _x:100 }, down = { _y:100 }, left = { _x:0 }, up = { _y:0 }, 
			clock = { _rotation:90 }, zero = { _x:0, _y:0, _rotation:0 }, shrink = { _yscale: 0};
			
		with (Minion.summon(mc1).to(right)) {
			chain(mc1).to(down)
			with(chain(mc2).to(right)) {
				chain(mc1).to(left)
				chain(mc2).to(down)
				with (chain(mc3).to(right)) {
					chain(mc3).to(clock).to(zero)					
					chain(mc4).to(right).wait( -2).to(zero)	
					chain(mc1).to(right).wait( -4).to(zero)
					with (chain(mc2).to(clock).wait( -6).to(zero)) {
						chain(mc1).wait().wait().to(shrink)
						chain(mc2).wait().to( { _x:110 } ).wait( -.5).to(shrink)
						chain(mc3).wait().to( { _x:220 } ).wait( -1).to(shrink)
						chain(mc4).wait().to( { _x:330 } ).wait( -1.5).to(shrink).whenDone(onDone)
					}
				}
			}
		}
	}
	/**
	 * This one shows how to use ease equations, 
	 * by simply transforming an ease equation to a Minion tween function and using it as default.
	 */
	static public function penner(onDone:Function):Void {
		Minion.EASE = Minion.ease(easeOut);
		Minion.TEMPO = 450;
		squares(onDone);
	}
	/**
	 * This one shows how to use bezier curves as Minion tween function.
	 */
	static public function bezier(onDone:Function):Void {
		Minion.EASE = Bezier.make([0, -1, 2, 1]);
		Minion.TEMPO = 450;
		squares(onDone);
	}
	//{ region utilities
		/**
		 * @author Robert Penner - This function is a copy of com.robertpenner.easing.Back.easeOut
		 * A mere copy seemed cleverer than redistributing the whole library
		 */
		static private function easeOut (t:Number, b:Number, c:Number, d:Number, s:Number):Number {
			if (s == undefined) s = 1.70158;
			return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
		}
		static private function clear():Void {
			var d:Number = _root.getNextHighestDepth();
			for (var i:Number = 0; i < d; i++) 
				_root.getInstanceAtDepth(i).removeMovieClip();
		}
		static private function makeSquare(length:Number, parent:MovieClip):MovieClip {
			parent = parent || layer;
			length = length || 100;
			var mc:MovieClip = parent.createEmptyMovieClip('square' + parent.getNextHighestDepth(), parent.getNextHighestDepth());
			mc.beginFill(0, 20);
			mc.lineStyle(1, 0, 50);
			mc.lineTo(0, length);
			mc.lineTo(length, length);
			mc.lineTo(length, 0);
			return mc;
		}	
	//} endregion
}